package tech.kunyuan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.kunyuan.utils.RedisUtils;

import java.io.Serializable;

/**
 * Description:
 *
 * @Author: ky
 * DateTime: 2022-10-19 16:14
 */
@RestController
public class WxController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisUtils redisUtils;

    @GetMapping
    public String index(){
        return "Index is ok";
    }


    @GetMapping("/redis")
    public String redis(){

        String key = "redis-test-02";
        Object result = redisUtils.get(key);
        if(result != null){
            System.out.println("get");
            System.out.println(result);
        }else{
            String value = "i am redis 02";
            redisUtils.set(key, value);
            System.out.println("set");
        }
        return "Cache test";
    }

    @GetMapping("/redis2")
    public String redis2(){
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        String key = "redis-test";
        Object result = operations.get(key);
        if(result != null){
            System.out.println("get");
            System.out.println(result);
        }else{
            String value = "i am redis";
            operations.set(key, value);
            System.out.println("set");
        }
        return "Cache test";
    }
}
